package org.hzz.locks;

import lombok.extern.slf4j.Slf4j;
import org.hzz.redis.JedisSupport;
import redis.clients.jedis.JedisPooled;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.LockSupport;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/8
 */
@Slf4j
public class RedisLockBackup {
    private JedisPooled jedis;

    public RedisLockBackup(){
        jedis = new JedisSupport("localhost",6379).getJedisPool();
    }


    public static void main(String[] args) {
        RedisLockBackup redisLock = new RedisLockBackup();
        log.info("{}",Thread.currentThread().getId());
        new Thread(() -> {
            log.info("{}",Thread.currentThread().getId());
        }).start();

        CompletableFuture.runAsync(() -> {
            log.info("{}",Thread.currentThread().getId());
        });

        CompletableFuture.runAsync(() -> {
            log.info("{}",Thread.currentThread().getId());
        }, Executors.newFixedThreadPool(100));

        LockSupport.park();
     //   redisLock.evalLua();
    }


    public void evalLua(){
        String hashKey = "books";
        jedis.hset(hashKey, "id:001", "Love Java");
        jedis.hset(hashKey, "id:002", "Love TypeScript");

        String script = """
            if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then
                redis.call('pexpire', KEYS[1], ARGV[1]);
                return 1;
            end;
            return 0;
        """;

        List<String> keys = Collections.singletonList(hashKey);
        List<String> argv = List.of(String.valueOf(30_000),"id:002");
        Object result = jedis.eval(script,keys,argv);
        System.out.println(result);
    }


    public void lock(){

    }

}
